package com.dgy.project.service.impl;

import java.util.List;

import com.dgy.common.constants.ProjectConstants;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import com.dgy.project.mapper.ProjectMemberMapper;
import com.dgy.project.domain.ProjectMember;
import com.dgy.project.service.IProjectMemberService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 参加者信息Service业务层处理
 *
 * @author ccc
 * @date 2025-03-20
 */
@Service
public class ProjectMemberServiceImpl implements IProjectMemberService
{
    @Autowired
    private ProjectMemberMapper projectMemberMapper;

    /**
     * 查询参加者信息
     *
     * @param id 参加者信息主键
     * @return 参加者信息
     */
    @Override
    public ProjectMember selectProjectMemberById(Long id)
    {
        return projectMemberMapper.selectProjectMemberById(id);
    }

    /**
     * 查询参加者信息列表
     *
     * @param projectMember 参加者信息
     * @return 参加者信息
     */
    @Override
    public List<ProjectMember> selectProjectMemberList(ProjectMember projectMember)
    {
        return projectMemberMapper.selectProjectMemberList(projectMember);
    }

    /**
     * 新增参加者信息
     *
     * @param projectMember 参加者信息
     * @return 结果
     */
    @Override
    public int insertProjectMember(ProjectMember projectMember)
    {
        projectMember.setCreateTime(DateUtils.getNowDate());
        return projectMemberMapper.insertProjectMember(projectMember);
    }

    /**
     * 修改参加者信息
     *
     * @param projectMember 参加者信息
     * @return 结果
     */
    @Override
    public int updateProjectMember(ProjectMember projectMember)
    {
        projectMember.setUpdateTime(DateUtils.getNowDate());
        return projectMemberMapper.updateProjectMember(projectMember);
    }

    /**
     * 批量删除参加者信息
     *
     * @param ids 需要删除的参加者信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectMemberByIds(Long[] ids)
    {
        return projectMemberMapper.deleteProjectMemberByIds(ids);
    }

    /**
     * 删除参加者信息信息
     *
     * @param id 参加者信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectMemberById(Long id)
    {
        return projectMemberMapper.deleteProjectMemberById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int changeProjectLeader(ProjectMember projectMember) {
        // 将原有负责人变更为普通成员
        projectMemberMapper.resetProjectLeader(projectMember.getProjectId());
        // 查询新负责人在成员信息中是否存在，若存在，更改其为负责人，否则新增一条信息
        ProjectMember searchMember = new ProjectMember();
        searchMember.setProjectId(projectMember.getProjectId());
        searchMember.setUserId(projectMember.getUserId());
        List<ProjectMember> projectMembers = projectMemberMapper.selectProjectMemberList(searchMember);
        if(projectMembers.isEmpty()){
            projectMember.setLeaderFlag(ProjectConstants.FLAG_YES);
            this.insertProjectMember(projectMember);
        }else{
            ProjectMember projectMemberNew = projectMembers.get(0);
            projectMemberNew.setLeaderFlag(ProjectConstants.FLAG_YES);
            this.updateProjectMember(projectMember);
        }
        return 1;
    }
}
